package cn.oa.service.impl;

import cn.oa.dao.AttendanceDao;
import cn.oa.dao.EmployeeDao;
import cn.oa.entity.Employee;
import cn.oa.service.EmployeeService;
import cn.oa.util.MyBatisUtil;
import cn.oa.util.Page;
import org.apache.ibatis.session.SqlSession;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class EmployeeServiceImpl implements EmployeeService{
    SqlSession sqlSession = MyBatisUtil.createSqlSession();
    EmployeeDao employeeDao = sqlSession.getMapper(EmployeeDao.class);
    AttendanceDao attendanceDao = sqlSession.getMapper(AttendanceDao.class);

    @Override
    public Employee findEmployee(String employeeNo, String password) throws SQLException {
        try{
            return employeeDao.findEmployee(employeeNo,password);
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            MyBatisUtil.closeSqlSession(sqlSession);
        }
    }

    @Override
    public void getEmployeesByPage(String name, Page pageObj) throws SQLException {
        try {
            int totalCount = employeeDao.getEmployeesCount(name);
            pageObj.setTotalCount(totalCount);
            if (totalCount > 0) {
                if (pageObj.getCurrPageNo() > pageObj.getTotalPageCount()) {
                    pageObj.setCurrPageNo(pageObj.getTotalPageCount());
                }
                List<Employee> empList = employeeDao.getEmployeesByPage(name,
                        (pageObj.getCurrPageNo()-1)* pageObj.getPageSize(),
                        pageObj.getPageSize());
                pageObj.setEmployeeList(empList);
            } else {
                pageObj.setCurrPageNo(0);
                pageObj.setEmployeeList(new ArrayList<Employee>());
            }

        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            MyBatisUtil.closeSqlSession(sqlSession);
        }
    }

    @Override
    public int updateEmployee(Employee employee) throws SQLException {
        try{
            return employeeDao.updateEmployee(employee);
        }catch (SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            MyBatisUtil.closeSqlSession(sqlSession);
        }
    }

    @Override
    public int addNewEmployee(Employee employee) throws SQLException {
        int result;
        try {
            if(employeeDao.getEmployeeByNo(employee.getEmployeeNo())>0){
                //该员工号已存在
                result = -1;
            }else{
                //新增员工
                result = employeeDao.addNewEmployee(employee);
            }
            sqlSession.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            sqlSession.rollback();
            throw e;
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return result;
    }

    @Override
    public Employee getEmployeeById(int id) throws SQLException {
        try{
            return employeeDao.getEmployeeById(id);
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            MyBatisUtil.closeSqlSession(sqlSession);
        }
    }

    @Override
    public int deleteEmployeeById(int id) throws SQLException {

        int result = 0;
        try{

            //删除员工的所有考核记录
            attendanceDao.deleteAttendanceByEmployeeId(id);
            //删除员工的信息
            result = employeeDao.deleteEmployeeById(id);
            sqlSession.commit();
        }catch(SQLException e){
            e.printStackTrace();
            sqlSession.rollback();
            throw e;
        }finally{
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return result;
    }

    @Override
    public int updatePassword(int id, String pwd) throws SQLException {
        try{
            return employeeDao.updatePasswordById(id,pwd);
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            MyBatisUtil.closeSqlSession(sqlSession);
        }
    }
}


